<template>
  <el-dialog :title="title" :visible="isShow" width="630px" @close="closeFn">
    <el-form ref="addForm" :rules="rules" :model="addForm">
      <el-form-item label="商品名称：" prop="skuName" label-width="150px">
        <el-input
          style="width: 85%"
          v-model="addForm.skuName"
          placeholder="请输入"
          maxlength="15"
          show-word-limit
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="品牌:" label-width="150px" prop="brandName">
        <el-input
          style="width: 85%"
          v-model="addForm.brandName"
          placeholder="请输入"
          maxlength="10"
          show-word-limit
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="商品价格（元）:" label-width="150px" prop="price">
        <el-input-number
          style="width: 85%"
          v-model="price"
          @change="priceChange"
          controls-position="right"
          placeholder="请输入"
          :min="0"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="商品类型:" label-width="150px" prop="classId">
        <el-select
          style="width: 85%"
          v-model="addForm.classId"
          placeholder="请选择"
        >
          <el-option
            v-for="item in skuClassList"
            :key="item.classId"
            :label="item.className"
            :value="item.classId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="规格:" label-width="150px" prop="unit">
        <el-input
          style="width: 85%"
          v-model="addForm.unit"
          placeholder="请输入"
          maxlength="10"
          show-word-limit
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="商品图片:" label-width="150px" prop="skuImage">
        <ImportImg v-model="addForm.skuImage" />
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-col :span="8">
          <el-button
            class="btn cancel"
            @click="closeFn"
            style="margin-right: 20px"
            >取消</el-button
          >
          <el-button class="btn confirm" type="primary" @click="submit"
            >确认</el-button
          >
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { searchSkuClassApi, addSkuApi, editSkuApi } from "@/api/sku";
import ImportImg from "@/views/vm/components/importImg.vue";
export default {
  props: {
    isShow: Boolean,
    title: String,
    skuNow: Object,
    isPrice: Number,
  },
  components: {
    ImportImg,
  },
  data() {
    return {
      addForm: {
        brandName: "",
        classId: "",
        price: "",
        skuImage: "",
        skuName: "",
        unit: "",
      },
      price: "",
      skuClassList: [],
      regionList: [],
      pageParams: {
        pageIndex: 1,
        pageSize: 1000,
      },
      rules: {
        skuName: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
        ],
        brandName: [
          { required: true, message: "请输入品牌名称", trigger: "blur" },
        ],
        price: [{ required: true, message: "请输入商品价格", trigger: "blur" }],
        classId: [
          { required: true, message: "请选择商品类型", trigger: "blur" },
        ],
        skuImage: [
          { required: true, message: "请上传商品图片", trigger: "blur" },
        ],
        unit: [{ required: true, message: "请输入商品规格", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getSkuClassList();
    if (this.title === "修改商品") {
      this.addForm = this.skuNow;
      this.price = this.isPrice;
    }
  },
  methods: {
    async getSkuClassList() {
      const res = await searchSkuClassApi();
      this.skuClassList = res.currentPageRecords;
    },

    priceChange(val) {
      this.addForm.price = val * 100;
    },

    closeFn() {
      this.$refs.addForm.resetFields();
      this.addForm = {
        brandName: "",
        classId: "",
        price: "",
        skuImage: "",
        skuName: "",
        unit: "",
      };
      this.$emit("update:isShow", false);
    },
    submit() {
      this.$refs.addForm.validate(async (pass) => {
        if (pass) {
          if (this.title === "新增商品") {
            await addSkuApi(this.addForm);
            this.$message.success("添加商品成功");
          } else {
            this.addForm.price = this.price * 100;
            await editSkuApi(this.addForm);
            this.$message.success("修改商品成功");
          }
          this.closeFn();
          this.$emit("searchSku");
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.btn {
  width: 80px;
  height: 36px;
  border: none;
  padding: 0;
  line-height: 36px;
}
.cancel {
  background-color: #fbf4f0;
  color: #7e7570;
}
.confirm {
  background-color: #ff6d29;
  color: #fff;
}
</style>
